﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    [AddComponentMenu("Nirvana/UI/Language/Prefab I18N")]
    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public sealed class PrefabI18N : Translatable
    {
        [Serializable]
        private struct BindLanguageObject
        {
            [Tooltip("The languaget type.")]
            public I18NLanguageType languagetype;
            [Tooltip("The translate Prefab.")]
            public GameObject translate;
        };

        /// <summary>
        /// 内置翻译表，不通过keyid获取，直接设置
        /// </summary>
        [SerializeField]
        [Tooltip("The bind Language Prefab list.")]
        private List<BindLanguageObject> binds = new List<BindLanguageObject>();

        private GameObject curInstance = null;
        public GameObject InstantObject
        {
            get { return curInstance; }
        }
        protected override void TranslateText()
        {
            this.UpdateText();
        }

        private  void GetTranslate()
        {
            I18NLanguageType language = I18N.Language;
            foreach (var f in binds)
            {
                if (f.translate != null && f.languagetype == language)
                {
                    curInstance = Instantiate(f.translate);
                    curInstance.transform.SetParent(this.transform);
                    return;
                }
      
            }
        }

        private void UpdateText()
        {
            if (Application.isPlaying)
            {
                if (curInstance != null) Destroy(curInstance);
                GetTranslate();
            }
        }

    }
}

